package com.green.service.common.log;

import java.lang.reflect.Field;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.green.common.util.DateUtil;
import com.green.common.util.utils.date.DateFormator;
import com.green.model.Entity;
import com.green.model.annotation.OperateLogField;

/**
 * 
 * @author Gou Lvbin
 * @time 2013-10-30
 */

public abstract class BaseOperateLogger implements OperateLogger {

	/**
	 * 获取对象中的值
	 * 
	 * @param field
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String getValue(Field field, Object obj) throws Exception {
		OperateLogField operateLogField = field
				.getAnnotation(OperateLogField.class);
		field.setAccessible(true);
		Object valueObj = field.get(obj);
		Object tempObj = null;
		String tempStr = "";
		if (StringUtils.isNotBlank(operateLogField.name()) && valueObj != null) {
			Field fieldSun = valueObj.getClass().getDeclaredField(
					operateLogField.name().toString());
			fieldSun.setAccessible(true);
			tempObj = fieldSun.get(valueObj);
		} else {
			tempObj = valueObj;
		}
		if (tempObj == null) {
			return tempStr;
		}
		tempStr = tempObj.toString();
		if (tempObj instanceof Date) {
			tempStr = DateUtil.format((Date) tempObj,
					DateFormator.YEAR_MONTH_DAY);
		}
		
		return tempStr;
	}

	@Override
	public abstract String getAddOperateContent(Field[] fields, Object obj)
			throws Exception;

	@Override
	public abstract String getUpdateOperateContent(Field[] fields, Object obj,
			Object historyObj) throws Exception;

	@Override
	public abstract String getDelOperateContent(Field[] fields,
			Object historyObj) throws Exception;
	
	@SuppressWarnings("rawtypes")
	public abstract void doLog(Entity entity) throws Exception;
	
}
